The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.
Changes 03
META.yml 35
Makefile.PL 11
SIGNATURE 1010
lib/Template/Plugin/JSON.pm 417
t/live.t 128
6 files changed (This is a version diff) 1964
@@ -1,3 +1,6 @@
+0.06
+	- add json_decode method patch by Leo Lapworth
+
 0.05
 	- Don't warn when no arguments are passed
 	- fix typo in deprecation warning
@@ -1,15 +1,17 @@
 --- #YAML:1.0
 name:               Template-Plugin-JSON
-version:            0.05
+version:            0.06
 abstract:           ~
 author:  []
 license:            unknown
 distribution_type:  module
 configure_requires:
     ExtUtils::MakeMaker:  0
+build_requires:
+    ExtUtils::MakeMaker:  0
 requires:
     JSON:           2.12
-    Mouse:          0
+    Moose:          0
     Template:       2.20
     Test::More:     0
     Test::use::ok:  0
@@ -17,7 +19,7 @@ no_index:
     directory:
         - t
         - inc
-generated_by:       ExtUtils::MakeMaker version 6.48
+generated_by:       ExtUtils::MakeMaker version 6.56
 meta-spec:
     url:      http://module-build.sourceforge.net/META-spec-v1.4.html
     version:  1.4
@@ -9,7 +9,7 @@ WriteMakefile(
     'NAME'         => 'Template::Plugin::JSON',
 	'VERSION_FROM' => 'lib/Template/Plugin/JSON.pm',
 	'PREREQ_PM' => {
-		'Mouse'         => '0',
+		'Moose'         => '0',
 		'Template'      => '2.20',
 		'Test::More'    => '0',
 		'Test::use::ok' => '0',
@@ -1,5 +1,5 @@
 This file contains message digests of all files listed in MANIFEST,
-signed via the Module::Signature module, version 0.55.
+signed via the Module::Signature module, version 0.61.
 
 To verify the content in this distribution, first make sure you have
 Module::Signature installed, then type:
@@ -14,17 +14,17 @@ not run its Makefile.PL or Build.PL.
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 
-SHA1 d0f7f2677cdd218f63b0c63be464b4bbad53c99f Changes
+SHA1 1c5729d0be6a89b3fa2e0307cc9cc0820631015c Changes
 SHA1 d72ae50825379c2c2f09b2f17a62e846da5c185d MANIFEST
 SHA1 190e9058eb9c6446a1a3f3ddf15b082f1ecde152 MANIFEST.SKIP
-SHA1 e2ceadde0da3f4eef94761b24aceba642bd37558 META.yml
-SHA1 060c9e1ce8acc7d38c673e4c3d4e4a0899890bee Makefile.PL
-SHA1 c21d1b2bbd9971017068b751ed1b62ca3421a623 lib/Template/Plugin/JSON.pm
-SHA1 b087f47c5f1bd8b084cfb51161e3c36f3ac53031 t/live.t
+SHA1 bbfe9331d5dd7a15fee91eabb3a8122b92c502e5 META.yml
+SHA1 18b544de103d1b1e59efe5a073ccdccdc318608a Makefile.PL
+SHA1 29271cbe1bf4ceb82e3079473f32cc362ce6f9cc lib/Template/Plugin/JSON.pm
+SHA1 12cb0a44925262e576c6f4f9110a91770af775a4 t/live.t
 -----BEGIN PGP SIGNATURE-----
-Version: GnuPG v1.4.7 (Darwin)
+Version: GnuPG/MacGPG2 v2.0.12 (Darwin)
 
-iD8DBQFJrbRHVCwRwOvSdBgRAteGAJ4tgeW3YLek8jdNJfljypIzHP32agCbB6LL
-LB9SveD004Eu2tT969bb7Sg=
-=onbs
+iEYEARECAAYFAkusmFEACgkQVCwRwOvSdBjLXwCgwV8llBzp8HLIMZnEz5kH7zjD
+IrgAoJuKoWsodvRHQV+Zr9B3D+CDwni6
+=/qJr
 -----END PGP SIGNATURE-----
@@ -1,15 +1,15 @@
 #!/usr/bin/perl
 
 package Template::Plugin::JSON;
-use Mouse;
+use Moose;
 
 use JSON ();
 
 use Carp qw/croak/;
 
-extends qw(Mouse::Object Template::Plugin);
+extends qw(Moose::Object Template::Plugin);
 
-our $VERSION = "0.05";
+our $VERSION = "0.06";
 
 
 has context => (
@@ -65,6 +65,12 @@ sub json {
 	$self->json_converter->encode($value);
 }
 
+sub json_decode {
+	my ( $self, $value ) = @_;
+
+	$self->json_converter->decode($value);
+}
+
 sub BUILD {
 	my $self = shift;
 	$self->context->define_vmethod( $_ => json => sub { $self->json(@_) } ) for qw(hash list scalar);
@@ -90,9 +96,16 @@ Template::Plugin::JSON - Adds a .json vmethod for all TT values.
 
 	</script>
 
+	or read in JSON
+
+	[% USE JSON %]
+	[% data = JSON.json_decode(json) %]
+	[% data.thing %]
+
 =head1 DESCRIPTION
 
-This plugin provides a C<.json> vmethod to all value types when loaded.
+This plugin provides a C<.json> vmethod to all value types when loaded. You
+can also decode a json string back to a data structure.
 
 It will load the L<JSON> module (you probably want L<JSON::XS> installed for
 automatic speed ups).
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 5;
+use Test::More tests => 9;
 
 use Template;
 use JSON;
@@ -29,3 +29,30 @@ is_deeply(
 	$vars,
 	"round tripping",
 );
+
+my $warnings = 0;
+
+local $SIG{__WARN__} = sub { $warnings++ };
+
+ok( Template->new->process(
+	\'[% USE JSON %][% SET foo = [ 1, 2, 3 ]; foo.json %]',
+	{},
+	\(my $blah),
+), "template processing" ) || warn( Template->error );
+
+is( $warnings, 0, "no warning" );
+
+# pass JSON to the template
+ok( Template->new->process(
+        \qq{[% USE JSON %][% USE Dumper -%]
+	    [%- val = JSON.json_decode(json_string) -%]
+	    [%- 'ok' IF val.blah.foo == 'bar' -%]},
+        my $json_vars = {
+            json_string => $out,
+        },
+        \( my $code_out ),
+    ),
+    "template processing"
+) || warn( Template->error );
+
+is($code_out,'ok', 'Match on extract');